package fun.xinnix.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisUtil {
    @Resource
    private RedisTemplate redisTemplate;

    public <T> void setCacheObject(final String key, T data) {
        redisTemplate.opsForValue().set(key, data);
    }

    public <T> void setCacheObject(final String key, T data, final Long timeout, final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, data, timeout, timeUnit);
    }

    public <T> T getObject(final String key) {
        ValueOperations<String, T> valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(key);
    }

    public boolean deleteObject(final String key) {
        return Boolean.TRUE.equals(redisTemplate.delete(key));
    }
}
